package com.appnet.android.truyencuoi;

import java.util.ArrayList;

import com.appnet.android.truyencuoi.db.DataBaseHelper;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class ListStoryActivity extends Activity {
	private ArrayList<ItemComic> mItems;
	private ListView mLvStories;
	private ListViewAdapter mAdapter;
	private DataBaseHelper mDbHelper;

	@SuppressLint("NewApi")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_story);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		Intent intent = getIntent();
		if (intent == null) {
			return;
		}
		int category = intent.getIntExtra(ComicConstant.EXTRA_CATEGORY, -1);
		if(category == -1) {
			return;
		}
		//
		setTitle(ComicUtils.getCategoryTitle(getApplicationContext(), category));
		mLvStories = (ListView) findViewById(R.id.lv_stories);
		//
		mDbHelper = new DataBaseHelper(getApplicationContext());

		mItems = mDbHelper.getItemByCategory(category);
		mAdapter = new ListViewAdapter(getApplicationContext(), mItems,
				new ItemOnClick() {

					@Override
					public void onClick(int type, int value) {
						Log.d(ComicConstant.PRE_TAG, "onclick: type: " + type + " value: " + value);
						Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
						intent.putExtra(ComicConstant.EXTRA_ID, value);
						startActivity(intent);
					}
				}, 0);

		mLvStories.setAdapter(mAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list_story, menu);
		return true;
	}

}
